#include <stdio.h>
#include <linux/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <string.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include "sound.h"




int sound_Read(int fd, unsigned char slave_addr, unsigned char reg_addr,unsigned char* data_buf, int data_len)
{
    struct i2c_rdwr_ioctl_data packet;
    struct i2c_msg messages[2];
    unsigned char reg_buf[1] = { reg_addr };
    int ret;

    /* Ð´¼Ä´æÆ÷µØÖ·½×¶Î */
    messages[0].addr = slave_addr;
    messages[0].flags = 0; // Ð´²Ù×÷
    messages[0].len = sizeof(reg_buf);
    messages[0].buf = reg_buf;

    /* ¶ÁÊý¾Ý½×¶Î */
    messages[1].addr = slave_addr;
    messages[1].flags = I2C_M_RD; // ¶Á²Ù×÷
    messages[1].len = data_len;
    messages[1].buf = data_buf;

    /* ×éÖ¯Êý¾Ý°ü */
    packet.msgs = messages;
    packet.nmsgs = 2;

    /* Ö´ÐÐ¸´ºÏ²Ù×÷ */
    if ((ret = ioctl(fd, I2C_RDWR, &packet)) < 0) {
        perror("I2C¶ÁÈ¡Ê§°Ü");
        return ret;
    }
    return 0;
}

/* ¸Ä½øºóµÄI2CÐ´º¯Êý */
int sound_Write(int fd, unsigned char slave_addr,unsigned char* data_buf, int data_len)
{
    struct i2c_msg message;
    struct i2c_rdwr_ioctl_data packet;
    int ret;

    message.addr = slave_addr;
    message.flags = 0;
    message.len = data_len;
    message.buf = data_buf;

    packet.msgs = &message;
    packet.nmsgs = 1;

    if ((ret = ioctl(fd, I2C_RDWR, &packet)) < 0) {
        perror("I2C写入失败");
        return ret;
    }
    return 0;
}

/* ¶ÁÈ¡ÓïÒô½á¹û */
int read_voice_result(int fd, unsigned char* result) {
    unsigned char data_buf[1]; // ½ö¶ÁÈ¡1×Ö½Ú
    int ret = sound_Read(fd, VOICE_MODULE_ADDR, RESULT_REG, data_buf, 1);

    if (ret == 0) {
        *result = data_buf[0];
        //printf("识别结果: 0x%02X\n", *result);
    }
    else {
        printf("读取结果失败\n");
    }
    return ret;
}


int set_voice_language(int fd, unsigned char lang) {
    unsigned char data_buf[2] = { PASSIVE_LANG_REG, lang };
    return sound_Write(fd, VOICE_MODULE_ADDR, data_buf, sizeof(data_buf));
}




//int main() {
//    int fd = -1;
//    unsigned char result;
//
//    // 1. ´ò¿ªÉè±¸
//    if ((fd = open("/dev/i2c-7", O_RDWR)) < 0) {
//        perror("打开设备失败");
//        return -1;
//    }
//
//    // 2. ³õÊ¼»¯ÉèÖÃ
//    if (set_voice_language(fd, 0x00) < 0) {
//        fprintf(stderr, "初始化失败\n");
//        close(fd);
//        return -1;
//    }
//
//    // 3. Ö÷Ñ­»·
//    printf("开始监听语音指令...\n");
//    while (1) {
//        int ret = read_voice_result(fd, &result);
//        if (ret == 0) {
//            if (result != 0x00) {
//                printf("检测到指令: 0x%02X\n", result);
//            }
//            else {
//                printf("未检测到有效指令\n");
//            }
//        }
//        else {
//            perror("读取失败");
//        }
//        usleep(500000); // 500ms
//    }
//
//    close(fd);
//    return 0;
//}



